<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Models\Specification;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;

class SpecificationsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('规格列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('规格详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑规格')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('添加规格')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Specification);

        $grid->id('Id');
        $grid->title('规格标题');
        $grid->name('规格名');
        $grid->sort('排序');
        $grid->column('规格值')->expand(function ($model) {
            $items = $model->items()->get()->map(function ($item) {
                return $item->only(['id', 'item', 'value', 'sort']);
            });
            return new Table(['ID', '规格值title', '规格值', '排序'], $items->toArray());
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Specification::findOrFail($id));

        $show->id('Id');
        $show->title('规格标题');
        $show->name('规格名');
        $show->sort('排序')->sortable();
        $show->created_at('创建时间')->sortable();
        $show->items('规格值', function ($items) {
            $items->resource('/admin/specification_items');
            $items->id();
            $items->item('规格值名')->editable();
            $items->value('规格值')->editable();
            $items->disableExport();
            $items->filter(function ($filter) {
                $filter->like('item','规格值名');
                $filter->like('value','规格值');
            });
        });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Specification);
        $form->text('title', '规格中文标题')->rules('required');
        $form->text('name', '规格名')->rules('required');
        $form->number('sort', '排序')->default(0);
        $form->hasMany('items','规格值', function (Form\NestedForm $form) {
            $form->text('item','规格值名')->rules('required');
            $form->text('value','规格值')->rules('required')->help('颜色值可以通过代码表查看：<a target="_blank" href="https://www.sioe.cn/yingyong/yanse-rgb-16/">代码查询</a>');
            $form->number('sort', '排序')->default(0);
        });
        return $form;
    }
}
